package com.csust.cg.mongodb.contorller;

import com.csust.cg.mongodb.domain.HttpStatus;
import com.csust.cg.mongodb.domain.Result;
import com.csust.cg.mongodb.domain.vo.DocumentVO;
import com.csust.cg.mongodb.domain.vo.QueryVO;
import com.csust.cg.mongodb.service.DocumentsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenguang
 */
@RequestMapping("/api")
@RestController
@ResponseBody
public class DocumentsController {

    @Autowired
    private DocumentsService documentsService;

    @PostMapping("/getDocumentsInfoList")
    public Result getDocumentsInfoList(@Validated QueryVO query, @RequestParam String connectionId) {
        if (StringUtils.isBlank(connectionId)) {
            new Result("连接ID不能为空！", HttpStatus.CONNECT_ID_IS_NULL);
        }
        if (StringUtils.isBlank(query.getDatabaseName())) {
            new Result("数据库名不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (StringUtils.isBlank(query.getCollectionName())) {
            new Result("集合名不能为空！", HttpStatus.BAD_REQUEST);
        }
        return documentsService.getDocumentsInfoList(query, connectionId);
    }

    @PostMapping("/getDocumentsInfo")
    public Result getDocumentsInfo(@RequestParam String connectionId, @RequestParam String databaseName, @RequestParam String collectionName) {
        if (StringUtils.isBlank(connectionId)) {
            new Result("连接ID不能为空！", HttpStatus.CONNECT_ID_IS_NULL);
        }
        if (StringUtils.isBlank(databaseName)) {
            new Result("数据库名不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (StringUtils.isBlank(collectionName)) {
            new Result("集合名不能为空！", HttpStatus.BAD_REQUEST);
        }
        return documentsService.getDocumentsInfo(connectionId, databaseName, collectionName);
    }

    @PostMapping("/insertDocument")
    public Result insertDocument(@RequestBody @Validated DocumentVO documents) {
        if (documents.getDocuments() == null || documents.getDocuments().size() < 1) {
            return new Result("集合信息不能为空！", HttpStatus.BAD_REQUEST);
        }
        return documentsService.insertDocument(documents.getDocuments(), documents.getConnectionId(), documents.getDatabaseName(), documents.getCollectionName());
    }

    @PostMapping("/dropDocument")
    public Result dropDocument(@RequestBody @Validated DocumentVO documents) {
        if (documents.getDocuments() == null || documents.getDocuments().size() < 1) {
            return new Result("集合信息不能为空！", HttpStatus.BAD_REQUEST);
        }
        return documentsService.dropDocumentsById(documents.getDocuments(), documents.getConnectionId(), documents.getDatabaseName(), documents.getCollectionName());
    }

    @PostMapping("/updateDocument")
    public Result updateDocument(@RequestBody @Validated DocumentVO documents) {
        if (documents.getDocuments() == null || documents.getDocuments().size() < 1) {
            return new Result("集合信息不能为空！", HttpStatus.BAD_REQUEST);
        }
        return documentsService.updateDocumentById(documents.getDocuments(), documents.getConnectionId(), documents.getDatabaseName(), documents.getCollectionName());
    }
}
